﻿using Himp.RL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling.Datas
{
    public class SamplePreparationDatavService : TaskSchedulingAppService, ISamplePreparationDatavService
    {
        private readonly SamplePreparationRecRepository _samplePreparationRecRepository;
        private IRepository<SamplePreparationTask, string> _samplePreparationTask;

        public SamplePreparationDatavService(SamplePreparationRecRepository samplePreparationRecRepository
            , IRepository<SamplePreparationTask, string> samplePreparationTask)
        {
            _samplePreparationRecRepository = samplePreparationRecRepository;
            _samplePreparationTask = samplePreparationTask;
        }

        public async Task<SamplePreparationDatavDto> GetSampleData()
        {
            var samplePreparationRecList = await _samplePreparationRecRepository.GetAllPersonNamesAsync();

            SamplePreparationDatavDto samplePreparationDatavDto = new SamplePreparationDatavDto();
            samplePreparationDatavDto.Tar6 = 1250;
            samplePreparationDatavDto.Tar3 = 700;
            samplePreparationDatavDto.Tar2 = 100;

            var samplePreparationTaskQuery = await _samplePreparationTask.GetQueryableAsync();
            samplePreparationTaskQuery = samplePreparationTaskQuery.Where(s => s.LastOperTime.Date == DateTime.Now.Date);
            samplePreparationTaskQuery = samplePreparationTaskQuery.Where(s => s.SampeWay == "自动");
            samplePreparationTaskQuery.OrderBy(s => s.TaskAssgnTime);
            var samplePreparationTaskList = samplePreparationTaskQuery.ToList();

            foreach (var item in samplePreparationTaskList)
            {
                var w61 = samplePreparationRecList.FirstOrDefault(s => s.SampleID == item.SampleCode && s.SampleType == 1);
                var w62 = samplePreparationRecList.FirstOrDefault(s => s.SampleID == item.SampleCode && s.SampleType == 2);
                var w31 = samplePreparationRecList.FirstOrDefault(s => s.SampleID == item.SampleCode && s.SampleType == 3);
                var w21 = samplePreparationRecList.FirstOrDefault(s => s.SampleID == item.SampleCode && s.SampleType == 4);
                var w22 = samplePreparationRecList.FirstOrDefault(s => s.SampleID == item.SampleCode && s.SampleType == 5);
                //item.SampleCode

                samplePreparationDatavDto.CurWeight6.Add(new int[] { w61 == null ? 0 : w61.SampleWeight, w62 == null ? 0 : w62.SampleWeight });
                samplePreparationDatavDto.CurWeight3.Add(new int[] { w31 == null ? 0 : w31.SampleWeight });
                samplePreparationDatavDto.CurWeight2.Add(new int[] { w21 == null ? 0 : w21.SampleWeight, w22 == null ? 0 : w22.SampleWeight });
            }

            samplePreparationDatavDto.FinishRateCur = samplePreparationRecList.Count;
            samplePreparationDatavDto.FinishRateTotal = samplePreparationTaskList.Count * 5;

            return samplePreparationDatavDto;
        }
    }
}
